package com.ft.oa.web.knowledgeBase;


import com.ft.oa.domain.dto.ApiResult;
import com.ft.oa.utils.DateUtils;
import com.ft.oa.utils.MinioUtil;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.net.URLEncoder;

@RestController
@RequestMapping("/kb/file")
public class FileController
{

    @Autowired
    MinioUtil minioUtil;

    /**
     * 上传一个文件
     *
     * @param uploadfile
     * @param objectName
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/uploadfile", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult fileupload(@RequestParam MultipartFile uploadfile,
                                @RequestParam(required = false) String objectName) throws Exception
    {
        if (ObjectUtils.isEmpty(objectName))
        {
            objectName = DateUtils.getDate();
            uploadfile.getSize();
        }
        minioUtil.uploadFile(uploadfile.getInputStream(), objectName + "/" + uploadfile.getOriginalFilename());
        return ApiResult.simpleSuccess();
    }

    /**
     * 列出所有的桶
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/listBuckets", method = RequestMethod.GET)
    @ResponseBody
    public ApiResult listBuckets() throws Exception
    {
        return ApiResult.successWithData(minioUtil.listBuckets());
    }

    /**
     * 递归列出一个桶中的所有文件和目录
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/listFiles", method = RequestMethod.GET)
    @ResponseBody
    public ApiResult listFiles() throws Exception
    {
        return ApiResult.successWithData(minioUtil.listFiles());
    }

    /**
     * 下载一个文件
     *
     * @param bucket
     * @param objectName
     * @param response
     * @throws Exception
     */
    @RequestMapping(value = "/downloadFile", method = RequestMethod.GET)
    @ResponseBody
    public void downloadFile(@RequestParam String bucket, @RequestParam String objectName,
                             HttpServletResponse response) throws Exception
    {
        InputStream stream = minioUtil.download(bucket, objectName);
        ServletOutputStream output = response.getOutputStream();
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(objectName.substring(objectName.lastIndexOf("/") + 1), "UTF-8"));
        response.setContentType("application/octet-stream");
        response.setCharacterEncoding("UTF-8");
        IOUtils.copy(stream, output);
    }

    /**
     * 删除一个文件
     *
     * @param bucket
     * @param objectName
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/deleteFile", method = RequestMethod.GET)
    @ResponseBody
    public ApiResult deleteFile(@RequestParam String bucket, @RequestParam String objectName) throws Exception
    {
        minioUtil.deleteObject(bucket, objectName);
        return ApiResult.simpleSuccess();
    }

}
